var basePage = require('../../common/BasePage');
Page({
    behaviors: [basePage],
    data: {
        goods_list: [],
        points: 0, //用户积分
        page: 1,
        totalPages: 0,
        loading: false,
        nomore: false
    },
    onLoad: function() {
        this.getintegral()
    },
    onShow: function() {
        this.getUserIntegral()
    },
    order_click(){
        console.log("前往订单中心");
        wx.navigateTo({
            url:'/pages/integral/mallOder/Index?from=mall',
        })
    },
    //点击订单中心
    integral_record() {
        wx.navigateTo({ url: '/pages/integral/integral_record/record' })
    },
    //点击商品item
    toExchange(e) {
        console.log("toExchange",e);
        const { id } = e.mark
        wx.navigateTo({ url: `/pages/integral/exchange_goods/exchange?id=${id}&points=${this.data.points}` })
    },
    //获取商品列表信息
    getintegral() {
        this.setData({ loading: true })
        let that = this
        this.request({
            url: this.data.host + '/home/mall/index',
            data: {
                page: this.data.page
            }
        }, function(res) {
            let { goods_list } = that.data;
            res.data.map(function(val){
                val.thumb=that.get_thumb(val.photo)
            })
            goods_list = [...goods_list, ...res.data]
            that.setData({ goods_list: goods_list, totalPages: res.pages, loading: false })
            if (res.current_page === res.pages) {
                that.setData({ nomore: true })
            }
        })
    },
    //获取用户积分信息
    getUserIntegral() {
        let that = this
        this.request({
            url: this.data.host + '/home/user/info'
        }, function(res) {
            that.setData({ points: res.data.points })
        })
    },
    login_reload() {
        this.getUserIntegral()
        this.getintegral()
    },
    onReachBottom: function() {
        if (this.data.page >= this.data.totalPages) return
        let { page } = this.data
        page++
        this.setData({ page })
        this.getintegral()
    },
})